﻿#include "MainWindowPropertyEditEntry.h"

#include <QMainWindow>
#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(MainWindow)

#include "SizeEditEntry.h"
#include "EnumEditEntry.h"
#include "BooleanEditEntry.h"
#include "FlagsEditEntry.h"

struct MainWindowPropertyEditEntry::MainWindowPropertyEditEntryPrivate {
    QPointer<QMainWindow> object;

    QPointer<SizeEditEntry> entryIconSize;
    QPointer<EnumEditEntry> entryToolButtonStyle;
    QPointer<BooleanEditEntry> entryAnimated;
    QPointer<BooleanEditEntry> entryDocumentMode;
    QPointer<EnumEditEntry> entryTabShape;
    QPointer<BooleanEditEntry> entryDockNestingEnabled;
    QPointer<FlagsEditEntry> entryDockOptions;
    QPointer<BooleanEditEntry> entryUnifiedTitleBarOnMac;
};

MainWindowPropertyEditEntry::~MainWindowPropertyEditEntry() { }

MainWindowPropertyEditEntry::MainWindowPropertyEditEntry(QWidget *parent) : PropertyEditEntry {parent} {
    p.reset(new MainWindowPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void MainWindowPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QMainWindow*>(object);
}

void MainWindowPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entryIconSize->setValue(p->object->iconSize());
    p->entryToolButtonStyle->setValue(p->object->toolButtonStyle());
    p->entryAnimated->setValue(p->object->isAnimated());
    p->entryDocumentMode->setValue(p->object->documentMode());
    p->entryTabShape->setValue(p->object->tabShape());
    p->entryDockNestingEnabled->setValue(p->object->isDockNestingEnabled());
    p->entryDockOptions->setValue(p->object->dockOptions());
    p->entryUnifiedTitleBarOnMac->setValue(p->object->unifiedTitleAndToolBarOnMac());
}

void MainWindowPropertyEditEntry::initUi() {
    p->entryIconSize = new SizeEditEntry;
    appendChildEntry(p->entryIconSize);
    connect(p->entryIconSize.data(), &SizeEditEntry::valueChanged, [=](const QSize &size) {
        p->object->setIconSize(size);
    });

    p->entryToolButtonStyle = new EnumEditEntry;
    appendChildEntry(p->entryToolButtonStyle);
    p->entryToolButtonStyle->setMetaEnum(QMetaEnum::fromType<Qt::ToolButtonStyle>());
    connect(p->entryToolButtonStyle.data(), &EnumEditEntry::valueChanged, [=](int value) {
        p->object->setToolButtonStyle(Qt::ToolButtonStyle(value));
    });

    p->entryAnimated = new BooleanEditEntry;
    appendChildEntry(p->entryAnimated);
    connect(p->entryAnimated.data(), &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setAnimated(value);
    });

    p->entryDocumentMode = new BooleanEditEntry;
    appendChildEntry(p->entryDocumentMode);
    connect(p->entryDocumentMode.data(), &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setDocumentMode(value);
    });

    p->entryTabShape = new EnumEditEntry;
    appendChildEntry(p->entryTabShape);
    p->entryTabShape->setMetaEnum(QMetaEnum::fromType<QTabWidget::TabShape>());
    connect(p->entryTabShape.data(), &EnumEditEntry::valueChanged, [=](int value) {
        p->object->setTabShape(QTabWidget::TabShape(value));
    });

    p->entryDockNestingEnabled = new BooleanEditEntry;
    appendChildEntry(p->entryDockNestingEnabled);
    connect(p->entryDockNestingEnabled.data(), &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setDockNestingEnabled(value);
    });

    p->entryDockOptions = new FlagsEditEntry;
    appendChildEntry(p->entryDockOptions);
    p->entryDockOptions->setMetaEnum(QMetaEnum::fromType<QMainWindow::DockOption>());
    connect(p->entryDockOptions.data(), &FlagsEditEntry::valueChanged, [=](int value) {
        p->object->setDockOptions(QMainWindow::DockOptions(value));
    });

    p->entryUnifiedTitleBarOnMac = new BooleanEditEntry;
    appendChildEntry(p->entryUnifiedTitleBarOnMac);
    connect(p->entryUnifiedTitleBarOnMac.data(), &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setUnifiedTitleAndToolBarOnMac(value);
    });
}

void MainWindowPropertyEditEntry::retranslateUi() {
    setTitle(tr("QMainWindow"));

    p->entryIconSize->setTitle(tr("iconSize"));
    p->entryToolButtonStyle->setTitle(tr("toolButtonStyle"));
    p->entryAnimated->setTitle(tr("animated"));
    p->entryDocumentMode->setTitle(tr("documentMode"));
    p->entryTabShape->setTitle(tr("tabShape"));
    p->entryDockNestingEnabled->setTitle(tr("dockNestingEnabled"));
    p->entryDockOptions->setTitle(tr("dockNestingEnabled"));
    p->entryUnifiedTitleBarOnMac->setTitle(tr("unifiedTitleBarOnMac"));
}
